﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizShpPageColumn : CustBizObject
    {
        private CDmShpPageColumn m_CDmShpPageColumn = new CDmShpPageColumn();
        private CDmShpPageColumn oDmShpPageColumn
        {
            get { return m_CDmShpPageColumn; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizShpPageColumn()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmShpPageColumn.SHP_PAGE_COLUMN);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["PAGE_COL_ID"]; 
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            } 
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            } 
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpPageColumnItem objtem = new CBizShpPageColumnItem())
            {
                DataSet ds = objtem.SelectByColId(this.GetRowStr(drw, KeyFieldName));
                for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
                {
                    ds.Tables[0].Rows[i].Delete();
                }
                objtem.Update(ds);
            }
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumnLevelOne objtem = new CBizShpPageColumnLevelOne())
            //{
            //    DataSet ds = objtem.SelectByColId(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //}
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        { 
            return true;
        }

        public DataSet SelectByIds(string strIdS)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  " + this.KeyFieldName + " IN  ('" + strIdS.Replace("'", "").Replace(",", "','") + "')");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }


        public DataSet SelectByPageNo(string strPageNo)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE   PAGE_NO  ='" + strPageNo + "' ORDER BY COL_SEQ_NUM ASC");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }
         
    }
}
